package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserDrugListRequest;
import com.frogs.ucenter.v1.request.UserDrugPartialListRequest;
import com.frogs.ucenter.v1.request.UserDrugShowRequest;
import com.frogs.ucenter.v1.service.UserDrugService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"UserDrugController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UserDrugController extends BaseController3 {
	
	@Resource
	private UserDrugService userDrugService;
	
    private static final String ROOT = "/drug";
    private static final String DRUG_DETAIL = ROOT + DETAIL;
    
    private static final String DRUG_LIST = ROOT + LIST;
    private static final String DRUG_PARTIAL_LIST = ROOT + PARTIAL_LIST;
	
	@RequestMapping(DRUG_LIST)
	public Response drugList(HttpServletRequest httpRequest) {
		Request request = new UserDrugListRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userDrugService.listDrugs(request);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	
	@RequestMapping({DRUG_DETAIL})
	public Response drugDetail(HttpServletRequest httpRequest) {
		Request request = new UserDrugShowRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userDrugService.showDrugs(request);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		
		return response;
	}
	
	@RequestMapping(DRUG_PARTIAL_LIST)
	public Response drugPartialList(HttpServletRequest httpRequest) {
		Request request = new UserDrugPartialListRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userDrugService.listDrugsPartlist(request);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		
		return response;
	}
}
